package pe.edu.unmsm.fisi.sisconteg.repository.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;

import pe.edu.unmsm.fisi.sisconteg.domain.model.Alumno;
import pe.edu.unmsm.fisi.sisconteg.repository.dao.AlumnoDAO;

@Repository("alumnoDAO")
public class AlumnoDAOImpl implements AlumnoDAO {

	private SessionFactory sessionFactory;

	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@Override
	public Alumno getAlumnoxCodigo(String codigoAlumno)
			throws DataAccessException {
		Alumno alumno = null;
		alumno = (Alumno) sessionFactory.getCurrentSession()
				.createCriteria(Alumno.class)
				.add(Restrictions.eq("codigoAlumno", codigoAlumno))
				.uniqueResult();

		return alumno;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Alumno> getAlumnosxNombres(String nombres)
			throws DataAccessException {
		List<Alumno> alumnos = new ArrayList<Alumno>();

		alumnos.addAll(sessionFactory.getCurrentSession()
				.createCriteria(Alumno.class)
				.add(Restrictions.like("nombres", nombres)).list());
		
		alumnos.addAll(sessionFactory.getCurrentSession()
				.createCriteria(Alumno.class)
				.add(Restrictions.like("apPaterno", nombres)).list());
		
		alumnos.addAll(sessionFactory.getCurrentSession()
				.createCriteria(Alumno.class)
				.add(Restrictions.like("apMaterno", nombres)).list());

		return alumnos;
	}

}
